<?php
namespace App\controllers;
use App\models\Machine;
use App\models\Server;
use Mini\core\Controller;
use Mini\core\Application;

class NascheckController extends Controller
{
    public $dirs = [];

    public function __construct()
    {
        $this->dirs = $this->dirToArray(Application::$config['data_path']);
    }

    public function index()
    {
        // load views
        require APPLICATION . 'views/_templates/header.php';
        require APPLICATION . 'views/nascheck/index.php';
        require APPLICATION . 'views/_templates/footer.php';
    }

    public function dirToArray($path)
    {
        $result = array(); 
        $cdir = scandir($path, SCANDIR_SORT_DESCENDING);

        foreach ($cdir as $value) 
        { 
            if (!in_array($value, array(".",".."))) 
            { 
                if (is_dir($path . DIRECTORY_SEPARATOR . $value)) 
                { 
                    $result[] = $value;
                } 
            } 
        } 
        return $result; 
    } 

    public function getWalter()
    {
        $walter = [];
        $dirs = $this->dirs;
        $current = trim($_GET["time"]);
        if ($current == "" && count($dirs) == 0) {
            $current = date('m-d');
        } else {
            $current = $dirs[0];
        }
        $walterLog = Application::$config['data_path'] . $current . "/walter.log";
        exec("cat $walterLog | grep Total | cut -d: -f 2", $total);
        exec("cat $walterLog | grep Used | cut -d: -f 2", $used);
        exec("cat $walterLog | grep Free | cut -d: -f 2", $free);
        $perf = [];
        $perf[0] = $used[0];
        $perf[1] = $free[0];
        $perf[2] = intval($total[0]) - intval($free[0]) - intval($used[0]);
        $perf[3] = round(floatval(1 -  ($free[0] / $total[0])) * 100, 2) . "%";
        $walter["perf"] = $perf;

        $standard = [];
        $standard[0] = $used[1];
        $standard[1] = $free[1];
        $standard[2] = intval($total[1]) - intval($free[1]) - intval($used[1]);
        $standard[3] = round(floatval(1 -  ($free[1] / $total[1])) * 100, 2) . "%";
        $walter["standard"] = $standard;
        return $walter;
    }

    public function collect()
    {
        //Attention: the current dir is : application
        $dirs = $this->dirs;
        $walter = $this->getWalter();

        $ret = [];
        $start = -1;

        $current = trim($_GET["time"]);
        if (count($dirs) == 0) {
            $this->index();
            return;
        }
        
        if (!in_array($current, $dirs)) {
            $this->index();
            return;
        }
        
        $path = Application::$config['data_path'] . $current. "/tsar.log";
        $myfile = fopen($path, "r") or die("Unable to open file!");
        $regExp_machine = "/.*\[SUCCESS\]\s*(\S+)/";
        $regExp_data ="/(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)/";
        while(!feof($myfile)) {
            $line = fgets($myfile);
            $data = [];
            if(preg_match_all($regExp_machine, $line, $data, PREG_PATTERN_ORDER)) {
                $start = $start + 1;
                $m = new Machine();                
                $m->host =  $data[1][0];
                $ret[] = $m;
            } else if (preg_match_all($regExp_data, $line, $data, PREG_PATTERN_ORDER)) {
                if($data[1][0] == "Time") continue;
                else {
                    $ret[$start]->startTime[] = $data[1][0];
                    $ret[$start]->cpu[] = $data[2][0];
                    $ret[$start]->memory[] = $data[3][0];
                    $ret[$start]->retran[] = $data[4][0];
                    if (strtoupper(substr($data[5][0], -1, 1)) == "K") {
                        $ret[$start]->bytIn[] = (float)substr($data[5][0], 0, strlen($str)-1) / 1024;
                    } else {
                        $ret[$start]->bytIn[] = (float)substr($data[5][0], 0, strlen($str)-1);
                    }  
                    if (strtoupper(substr($data[6][0], -1, 1)) == "K") {
                        $ret[$start]->bytOut[] = (float)substr($data[6][0], 0, strlen($str)-1) / 1024;
                    } else {
                        $ret[$start]->bytOut[] = (float)substr($data[6][0], 0, strlen($str)-1);
                    }  
                    /*
                    $ret[$start]->sda[] = $data[7][0];
                    $ret[$start]->sdb[] = $data[8][0];
                    $ret[$start]->sdc[] = $data[9][0];
                    $ret[$start]->sdd[] = $data[10][0];
                    $ret[$start]->sde[] = $data[11][0];
                    $ret[$start]->sdf[] = $data[12][0];
                    $ret[$start]->sdg[] = $data[13][0];
                    $ret[$start]->sdh[] = $data[14][0];
                    $ret[$start]->sdi[] = $data[15][0];
                    $ret[$start]->sdj[] = $data[16][0];
                    $ret[$start]->sdk[] = $data[17][0];
                    $ret[$start]->sdl[] = $data[18][0];
                    $ret[$start]->sdm[] = $data[19][0];
                    */
                    $ret[$start]->load[] = $data[20][0];
                }
            } else {
                continue;
            }
        }
        fclose($myfile);
        require APPLICATION . 'views/_templates/header.php';
        require APPLICATION . 'views/nascheck/index.php';
        require APPLICATION . 'views/_templates/footer.php';
    }
}
